﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Routing;

namespace Q42.Wheels.Mvc.Paging
{
  /// <summary>
  /// The central Paging object
  /// </summary>
  public class Paging
  {
    #region Constants

    public const string DefaultPagingName = "Paging";
    public const int DefaultPageSize = 10;
    public const string DefaultPaginationUserControlName = "PaginationUserControl";

    #endregion

    #region Constructors

    public Paging()
      : this(DefaultPagingName)
    {      
    }

    public Paging(string pagingName)
    {
      PagingName = pagingName;
      NumberPerPage = DefaultPageSize;
      PaginationUserControlName = DefaultPaginationUserControlName;
    }

    #endregion

    #region Properties

    public string PagingName { get; set; }

    public int CurrentPage { get; set; }
    public int NumberPerPage { get; set; }
    public int TotalNumber { get; set; }

    public string Sort { get; set; }
    public bool SortDescending { get; set; }

    public bool KeepPageAfterSort { get; set; }

    public IDictionary<string, string> VaryByParamsValues { get; set; }

    public string PaginationUserControlName { get; set; }

    public int NumberPages { get { return (int)Math.Ceiling((double)TotalNumber / (double)NumberPerPage); } }

    /// <summary>
    /// The name of the page GET parameter associated with this Paging object
    /// </summary>
    public string PageParameterName
    {
      get { return String.Format("{0}.Page", PagingName); }
    }

    /// <summary>
    /// The name of the sorting field GET parameter associated with this Paging object
    /// </summary>
    public string SortParameterName
    {
      get { return String.Format("{0}.Sort", PagingName); }
    }

    /// <summary>
    /// The name of the sorting direction GET parameter associated with this Paging object
    /// </summary>
    public string SortDescendingParameterName
    {
      get { return String.Format("{0}.SortDesc", PagingName); }
    }

    public bool IsFirstPage
    {
      get { return CurrentPage == 0; }
    }

    public bool IsLastPage
    {
      get { return CurrentPage == NumberPages - 1; }
    }

    #endregion
  }
}
